class Solution(object):
    def minDays(self, bloomDay, m, k):
        left = min(bloomDay)
        right = max(bloomDay)
        n = len(bloomDay)
        if m * k > n:
            return -1

        def search(val):
            i = 0
            j = 0
            c = 0
            while j < n:
                if bloomDay[j] <= val:
                    if j - i + 1 == k:
                        i = j + 1
                        c += 1
                else:
                    i = j + 1
                j += 1
            return c >= m

        while left < right:
            mid = (left + right) // 2
            if search(mid):
                right = mid
            else:
                left = mid + 1

        if search(left):
            return left
        else:
            return -1